package com.lcu.student.service.impl;

import com.lcu.Result.Result;
import com.lcu.student.entity.PageBean;
import com.lcu.student.entity.Teacher;
import com.lcu.student.mapper.TeacherMapper;
import com.lcu.student.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 查询所有教师及编号
     *
     * @return
     */
    @Override
    public Result selectTeacher() {
        List<Teacher> teachers = teacherMapper.selectTeacher();
        return Result.success(teachers);
    }

    /*
     *  带条件的分页查询
     */
    @Override
    public Result selectByPage(int currentPage, int pageSize, Teacher teacher) {
        int begin=(currentPage-1)*pageSize;
        int size=pageSize;

        String teacherName = teacher.getTeacherName();
        if (teacherName != null && teacherName.length()!=0){
            teacher.setTeacherName("%"+teacherName+"%");
        }
        String teacherNumber = teacher.getTeacherNumber();
        if (teacherNumber != null && teacherNumber.length()!=0){
            teacher.setTeacherNumber("%"+teacherNumber+"%");
        }

        List<Teacher> row = teacherMapper.selectByPage(begin, size, teacher);

        int totalCount = teacherMapper.selectTotalCount(teacher);

        PageBean<Teacher> pageBean=new PageBean<>();
        pageBean.setRows(row);
        pageBean.setTotalCount(totalCount);

        return Result.success(pageBean);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(int id) {
        teacherMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(List<Integer> ids) {
        teacherMapper.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 添加数据
     * @param teacher
     * @return
     */
    @Override
    public Result addTeacher(Teacher teacher) {
        teacherMapper.addTeacher(teacher);
        return Result.success();
    }

    /**
     * 根据id修改数据
     * @param teacher
     * @return
     */
    @Override
    public Result updateById(Teacher teacher) {
        teacherMapper.updateById(teacher);
        return Result.success();
    }
}
